We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Nginx Redirect URLs HTTP to HTTPS

Nginx Redirect URLs HTTP to HTTPS with www or non-www in your URL. Redirecting a URL is necessary in Nginx to inform the visitor that the page they are requesting resides in another location. This also helps in SEO by preventing the duplication of the URL.

For example a domain name with the prefix www and non-www will be considered as different domains in the view of search engines, so it is necessary to redirect all non-www urls with www and vice versa.

In this guide we are going to learn how to redirect HTTP to HTTPS and also redirect non-www to www.

Redirect with www in Nginx

To redirect a url with www you can add the below code inside your server block.

server {
    listen [::]:80;
    listen 80;

    server_name domain.com www.domain.com;

    return 301 http://www.domain.com$request_uri;
}

server {
    listen [::]:80;
    listen 80;

    server_name www.domain.com; 

    #your configurations here
} 

The above configuration catches all HTTP requests for the domain.com and www.domain.com and redirects it to www.domain.com

Redirect to non-www in Nginx

To redirect a url with www you can add the below code inside your server block.

server {
    listen [::]:80;
    listen 80;

    server_name domain.com www.domain.com;

    return 301 http://domain.com$request_uri;
}

server {
    listen [::]:80;
    listen 80;

    server_name domain.com; 

    #your configurations here
} 

The above configuration catches all HTTP requests for the domain.com and www.domain.com and redirects it to domain.com without www.

Redirect to HTTPS with www

To redirect a url with HTTPS you need to install Let’s Encrypt SSL and you can add the below code inside your server block.

server {
    listen [::]:80;
    listen 80;

    server_name domain.com www.domain.com;

    return 301 https://www.domain.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name domain.com;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
 
    root /var/www/html/domain.com/public/;
    index index.php index.html;

    return 301 https://www.domain.com$request_uri;
} 

 server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name www.domain.com; 

    #your configurations here
} 

The above configuration catches all HTTP requests for the domain.com and www.domain.com and redirects it to www.domain.com . It also catches the non-www HTTPS requests and redirect it to HTTPS with www.

Redirect to HTTPS with non-www

To redirect a url with HTTPS you need to install Let’s Encrypt SSL and you can add the below code inside your server block.

server {
    listen [::]:80;
    listen 80;

    server_name domain.com www.domain.com;

    return 301 https://domain.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name www.domain.com;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
 
    root /var/www/html/domain.com/public/;
    index index.php index.html;

    return 301 https://domain.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name domain.com; 

    #your configurations here
}

The above configuration catches all HTTP requests for the domain.com and www.domain.com and redirects it to domain.com . It also catches the www HTTPS requests and redirect it to HTTPS without www.

Prepare yourself for a role working as an Information Technology Professional with Linux operating system

Conclusion

Now you have learned how to redirect with www and without www and to HTTPS in Nginx.

Cart

Your Cart is Empty

Back To Shop